package geektime.concurrent.part2.concurrencycontrol;

import java.time.Instant;
import java.util.concurrent.CountDownLatch;

public class CountDownLatchWorker extends Thread {
    private String name;

    private CountDownLatch latch;

    public CountDownLatchWorker(String name, CountDownLatch latch) {
        setName(name); // 直接通过构造方法设置线程名称
        this.latch = latch;
    }

    @Override
    public void run() {
        System.out.printf("[%s]已创建，阻塞在latch\n", getName());
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.printf("[%s]开始执行了, time=%s\n", getName(), Instant.now());
    }
}
